//建立表关联
require("./src/models/relation");
//与数据库同步
require("./src/models/sync");

const express = require("express");
const bodyParser = require("body-parser");
const { mkdir } = require("./src/tools");
const os = require("os");
const fileUrl = require("./src/tools/fileUrl");
const logMiddleware = require("./src/tools/log");
const cors = require("cors");
const verifyTokenMiddleware = require("./src/routes/verifyTokenMiddleware");
// const https = require("https");
// const path = require("path");
// const fs = require("fs");
//创建一个express应用
const app = express();

app.use(bodyParser.urlencoded({ limit: "5000mb" }));
//解析JSON格式的请求体数据
app.use(bodyParser.json({ limit: "5000mb", extended: true }));
app.use(cors());

const UserRoute = require("./src/routes/api/UserManager");
const SymptomSubRoute = require("./src/routes/api/SymptomSubManager");
const SymptomRoute = require("./src/routes/api/SymptomManager");
const exportPDFManager = require("./src/routes/api/exportPDF");
const uploadRoute = require("./src/routes/api/uploadRoute");
const ConfigRoute = require("./src/routes/api/readConfig");
const SearchRoute = require("./src/routes/api/SearchInfoManager");
const UserSymptomRoute = require("./src/routes/api/UserSymptomManager");
const UserSymptomSubRoute = require("./src/routes/api/UserSymptomSubManager");
const logRoute = require("./src/routes/api/logInfoManager");
const ModelRoute = require("./src/routes/api/ModelManager");
const word2Pdf = require("./src/routes/api/word2pdf")
// const downVideoRoute = require("./src/routes/api/downVideo");
//下面接口已用c++重写
// const exportImgManager = require("./src/routes/api/exportImgManager2");
// const RecordRoute = require("./src/routes/api/RecordInfoManager");//检查记录服务
// const uploadImgRoute = require("./src/routes/api/uploadImgRoute");//后端上传图片接口
// const uploadVideoRoute = require("./src/routes/api/uploadVideoRoute");//后端上传视频接口
// const ImgParamRoute = require("./src/routes/api/ImgParamManager");//图片参数
// const uploadMeasureRoute = require("./src/routes/api/uploadMeasureImg");//测量图片上传
// const VideoParamsRotue = require("./src/routes/api/VideoParam");//视频参数
// const remoteVideoRotue = require("./src/routes/api/uploadRemoteVideo")//远程上传视频

//设置允许跨域访问该服务.
app.all("*", function (req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "Content-Type");
  res.header("Access-Control-Allow-Methods", "*");
  next();
});

let uploadStaticPath = `${fileUrl.uploadStaticPath}`; //图片上传路径
let videoStaticPath = `${fileUrl.videoStaticPath}`; //视频路径
app.use("/pubilc/uploads", express.static(videoStaticPath));
const system = os.platform();
if (system === "linux") {
  app.use("/pubilc/uploads", express.static(videoStaticPath));
}
mkdir(uploadStaticPath);

app.use(logMiddleware());

//托管静态资源
// app.use(express.static(staticPath));
app.use(express.static(__dirname + "/pubilc", { index: "index.html" }));
app.use("/pubilc/uploads", express.static(uploadStaticPath));

//用户中间件
app.use("/api/um", UserRoute);
//上传中间件
app.use("/api/upload", uploadRoute);
//检查分类中间件
app.use("/api/symptomSub", verifyTokenMiddleware, SymptomSubRoute);
//症状中间件
app.use("/api/symptom", verifyTokenMiddleware, SymptomRoute);
//导出pdf中间件
app.use("/api/exportPDF", verifyTokenMiddleware, exportPDFManager);
//错误处理中间件
app.use(require("./src/routes/errorMiddleware"));
//读取配置中间件
app.use("/api/readConfig", ConfigRoute);
//常用词库
app.use("/api/search", verifyTokenMiddleware, SearchRoute);
//检查分类中间件-用户
app.use("/api/userSymptomSub", UserSymptomSubRoute);
//症状中间件-用户
app.use("/api/userSymptom", UserSymptomRoute);
app.use("/api/log", logRoute);
//软件版本
app.use("/api/model", ModelRoute);
//word转pdf
app.use("/api/uploadWord", word2Pdf);
//下载视频压缩包
// app.use("/api/video", downVideoRoute);

//导出图片及视频中间件
// app.use("/api/exportImg", exportImgManager);
// //检查记录中间件
// app.use("/api/recordInfo",RecordRoute);
// //上传图片中间件-c++
// app.use("/api/uploadImgRoute", uploadImgRoute);
// //上传视频中间件-c++
// app.use('/api/uploadVideoRoute',uploadVideoRoute)
// //图片参数中间件
// app.use("/api/imgParam", ImgParamRoute);
// //测量图片上传
// app.use("/api/uploadMeasure",uploadMeasureRoute)
// //视频参数
// app.use('/api/videoParam',VideoParamsRotue)
// //远程上传视频
// app.use('/api/uploadRemoteVideo',remoteVideoRotue);

const port = "7008";
app.listen(port, () => {
  console.log("listening on 7008");
});
// var privateCrt = fs.readFileSync(
//   path.join(process.cwd(), "cert/diag.cobotsys.com_bundle.crt"),
//   "utf8"
// );
// var privateKey = fs.readFileSync(
//   path.join(process.cwd(), "cert/diag.cobotsys.com.key"),
//   "utf8"
// );
// const credentials = {
//   key: privateKey,
//   cert: privateCrt,
// };
// const HTTPS_PORT = 443;
// const httpsServer = https.createServer(credentials, app);
// httpsServer.listen(HTTPS_PORT, () => {
//   console.log(`HTTPS Server running on port ${HTTPS_PORT}`);
// });
